package dfbz.com.service.impl;

import dfbz.com.dao.UserDao;
import dfbz.com.dao.UserInfoDao;
import dfbz.com.model.Student;
import dfbz.com.model.User;
import dfbz.com.model.UserInfo;
import dfbz.com.service.UserService;

import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDao();
    private UserInfoDao userInfoDao=new UserInfoDao();

    @Override
    public boolean findUserByNameAndPwd(String username,String password) {
        User user = userDao.findUserByName(username);
        if(null != user){
            //数据库查找到了用户，接下来要比较密码
            if(password.equals(user.getPassword())){
                //密码一致，登录成功
                return true;
            }
        }
        //其余情况都属于登录失败
        return false;
    }

    @Override
    public User findUserBaseInfo(String username) {
        User user = userDao.findUserByName(username);
        if(null != user){
            user.setEmail(null);
            user.setPassword(null);
        }
        return user;
    }
    @Override
    public String finddept(String username) {
        return userDao.findDept(username);
    }

    @Override
    public List<Map<String,Object>> search(String search ,int page) {
        // 你这里不能返回一个String， 而因该是一个List<>
        return userDao.search(search,page);
    }

    @Override
    public List<Map<String, Object>> searchTotal(String search) {
        return userDao.searchTotal(search);
    }

    @Override
    public User findUserEmail(String email) {
        return userDao.findUserEmail(email);
    }

    @Override
    public List<User> findAll() {
       return  userDao.findAll(User.class);
    }




    @Override
    public void add(User user) {
        User user1 = new User();
       user1.setId(user.getId());
        user1.setUsername(user.getUsername());
        user1.setPassword(user.getPassword());
        user1.setIsSecret(user.getIsSecret());
        user1.setEmail(user.getEmail());
        user1.setDeptId(user.getDeptId());

        userDao.add(user1);
    }

    @Override
    public void restnewpsw(Integer id, String newpassword) {
        User user = new User();
        user.setId(id);
        user.setPassword(newpassword);
        userDao.update(user);
    }
}
